/*
 * The contents of this web application are subject to the Mozilla Public License Version 
 * 1.1 (the "License"); you may not use this web application except in compliance with 
 * the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, 
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
 * for the specific language governing rights and limitations under the License.
 * 
 * The Original Code is owned by and the Initial Developer of the Original Code is 
 * Composite A/S (Danish business reg.no. 21744409). All Rights Reserved
 * 
 * Section 11 of the License is EXPRESSLY amended to include a provision stating 
 * that any dispute, including but not limited to disputes related to the enforcement 
 * of the License, to which Composite A/S as owner of the Original Code, as Initial 
 * Developer or in any other role, becomes a part to shall be governed by Danish law 
 * and be initiated before the Copenhagen City Court ("K�benhavns Byret")            
 */

//-------------------------------------------------
//                youtube playlist jquery plugin
//-------------------------------------------------

jQuery.fn.ytplaylist = function (_options) {

	// default settings
	var options = jQuery.extend({
		holderId: '_ytvideo',
		playerHeight: '300',
		playerWidth: '450',
		thumbSize: 'small',
		showInline: true,
		autoPlay: true,
		showRelated: true,
		allowFullScreen: true
	}, _options);

	var currentVideoHtml = "";

	return this.each(function () {

		var selector = $(this);
		//throw a youtube player in
		function play(id) {
			var autoPlay = "";
			var showRelated = "&rel=0";
			var fullScreen = "";
			if (options.autoPlay) autoPlay = "&autoplay=1";
			if (options.showRelated) showRelated = "&rel=1";
			if (options.allowFullScreen) fullScreen = "&fs=1";

			var html = '';

			html += '<object height="' + options.playerHeight + '" width="' + options.playerWidth + '">';
			html += '<param name="movie" value="http://www.youtube.com/v/' + id + autoPlay + showRelated + fullScreen + '"> </param>';
			html += '<param name="wmode" value="transparent"> </param>';
			if (options.allowFullScreen) {
				html += '<param name="allowfullscreen" value="true"> </param>';
			}
			html += '<embed src="http://www.youtube.com/v/' + id + autoPlay + showRelated + fullScreen + '"';
			if (options.allowFullScreen) {
				html += ' allowfullscreen="true" ';
			}
			html += 'type="application/x-shockwave-flash" wmode="transparent"  height="' + options.playerHeight + '" width="' + options.playerWidth + '"></embed>';
			html += '</object>';
			window.location.hash = "#show" + id;
			return html;

		};


		//grab a youtube id from a (clean, no querystring) url (thanks to http://jquery-howto.blogspot.com/2009/05/jyoutube-jquery-youtube-thumbnail.html)
		function youtubeid(url) {
			var ytid = url.match("[\\?&]v=([^&#]*)");
			ytid = ytid[1];
			return ytid;
		};

		//load inital video
		var firstVid = selector.children("div:first-child").addClass("currentvideo").children("a").attr("href");
		//$("#" + options.holderId + "").html(play(youtubeid(firstVid)));

		//load video on request
		selector.find(".video-link").click(function () {

			if (options.showLightbox) {
				$("div.currentvideo").removeClass("currentvideo");
				showHtmlLightbox(this, {
					width: parseInt(options.playerWidth) + 20,
					height: parseInt(options.playerHeight) + 20,
					html: play(youtubeid($(this).attr("href")))
				});
				$(this).parents("div.video-list-item").addClass("currentvideo");
			}
			else {
				$("div.currentvideo").show();
				$("div.currentvideo").next("object").remove();
				$("div.currentvideo").removeClass("currentvideo");
				$(this).parents("div.video-list-item").addClass("currentvideo").after(play(youtubeid($(this).attr("href"))));
				$("div.currentvideo").hide();
			}
			return false;
		});

	});

};